package org.itboys.mobile.service.mysql.front;

import org.itboys.commons.utils.time.TimeUtils;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.product.SundayProductMapper;
import org.itboys.mobile.dao.promotion.SundayTeamGroupMemberMapper;
import org.itboys.mobile.dto.promotion.SundayTeamGroupDto;
import org.itboys.mobile.dto.promotion.SundayTeamGroupMemberDto;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.entity.mysql.promotion.SundayTeamGroup;
import org.itboys.mobile.entity.mysql.promotion.SundayTeamGroupMember;
import org.itboys.mobile.service.mongo.front.SundayFrontMemberService;
import org.itboys.mysql.service.BaseMySqlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.*;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 描述：圣代_商品_拼团组_service_移动端 。
 */
@Service
public class SundayFrontTeamGroupService extends BaseMySqlService<SundayTeamGroup> {
    @Autowired
    private SundayFrontMemberService memberService;
    @Autowired
    private SundayProductMapper productMapper;
    @Autowired
    private SundayTeamGroupMemberMapper groupMemberMapper;
    @Autowired
    private ResourceHolder resourceHolder;

    /**
     * 发起/参与拼团。支付回掉是创建
     * @param memberId
     * @param productId
     * @param orderId
     * @param groupId
     */
    public  void join(Long memberId,Long productId,Long orderId,Long groupId){
        SundayTeamGroup group = null;
        SundayMember member =memberService.getById(memberId);

        //参与别人的拼团
        if(LongUtil.isNotZreo(groupId)){
            group=super.find(groupId);
            //判断是否超出人数
            /*if(group.getSurplusNumber()<=0){
                throw new ErrorException("该团已经拼满或该团中有人未付款");
            }else{
                group.setSurplusNumber(group.getSurplusNumber()-1);
            }*/
            group.setSurplusNumber(group.getSurplusNumber()-1);
            super.update(group);
        //自己单独开团
        }else{
            SundayProduct product=productMapper.find(productId);
            group=new SundayTeamGroup();
            group.setMemberId(member.getId());
            group.setMemberName(member.getNickName());
            group.setMemberImage(member.getImage());
            group.setMaxNumber(product.getTeamMemberNum());
            group.setSurplusNumber(group.getMaxNumber()-1);
            group.setStatus(SundayCommonConstants.team_status_ptz);
            group.setHour(product.getTeamHour());
            //不要用date,使用calendar
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.HOUR_OF_DAY,group.getHour());
            group.setDeadTime(calendar.getTime());
            group.setProductId(productId);
            super.insert(group);
        }
        //新增拼团人
        SundayTeamGroupMember groupMember = new SundayTeamGroupMember();
        groupMember.setGroupId(group.getId());
        groupMember.setMemberId(member.getId());
        groupMember.setMemberName(member.getNickName());
        groupMember.setMemberImage(member.getImage());
        groupMember.setOrderId(orderId);
        groupMember.setProductId(group.getProductId());
        groupMember.setDeadTime(group.getDeadTime());
        groupMember.setStatus(SundayCommonConstants.team_status_wfk);
        groupMemberMapper.insert(groupMember);

    }


    /**
     * 不分页获取商品的拼团组和对应的人员
     * @param request
     * @param productId
     * @return
     */
    public List<SundayTeamGroupDto> getGroup(HttpServletRequest request,Long productId,Integer limit) {
        Map<String,Object> param = new HashMap<String,Object>();
        //param.put("isDeleted",SundayCommonConstants.NO);
        param.put("productId",productId);
        param.put("status",SundayCommonConstants.team_status_ptz);
        //2018年1月19日，增加取出条数
        if(limit!=null&&limit>0){
            param.put("row_start",0);
            param.put("row_size",5);
        }
        List<SundayTeamGroupDto> groupDtos =packageGroupDtoBatch(super.select(param));
        //2018年2月3日，暂时删除
        /*if(ListUtil.isNotNull(groupDtos)){
            List<Long> groupIds = new ArrayList<>();
            param.put("groupIds",groupIds);
            param.put("status",SundayCommonConstants.team_status_ptz);
            List<SundayTeamGroupMemberDto> groupMemberDtos=packageGroupMemberDtoBatch(groupMemberMapper.select(param));

            for(SundayTeamGroupDto groupDto:groupDtos){
                List<SundayTeamGroupMemberDto> children = new ArrayList<>();
                for(SundayTeamGroupMemberDto groupMemberDto:groupMemberDtos){
                    if(groupDto.getId()==groupMemberDto.getGroupId().longValue()){
                        children.add(groupMemberDto);
                    }
                }
                groupDto.setChildren(children);
            }
        }*/
        return groupDtos;
    }

    /**
     * 组装DTO
     * @param groups
     * @return
     */
    public List<SundayTeamGroupDto> packageGroupDtoBatch(List<SundayTeamGroup> groups){
        List<SundayTeamGroupDto> dtos = new ArrayList<>();
        String imgRoot = resourceHolder.getStringValue("imgRoot");
        for(SundayTeamGroup group:groups){
            dtos.add(new SundayTeamGroupDto( group.getId(),group.getCreateTime(),group.getCreateDate(),
                   group.getMemberId(), group.getMemberName(), imgRoot+group.getMemberImage(),
                    group.getSurplusNumber(),group.getMaxNumber(), TimeUtils.formatToTime(group.getDeadTime())));
        }
        return dtos;
    }
    public List<SundayTeamGroupMemberDto> packageGroupMemberDtoBatch(List<SundayTeamGroupMember> members){
        List<SundayTeamGroupMemberDto> dtos = new ArrayList<>();
        String imgRoot = resourceHolder.getStringValue("imgRoot");
        for(SundayTeamGroupMember member:members){
            dtos.add(new SundayTeamGroupMemberDto(
                    member.getMemberId(),member.getCreateTime(),member.getCreateDate(),
                    member.getGroupId(),member.getMemberId(), member.getMemberName(), imgRoot+member.getMemberImage()));
        }
        return dtos;
    }

    /**
     * 查询用户是否重复参团
     * @param memberId
     * @param groupId
     * @return
     */
    public boolean checkIsJoin(Long memberId,Long groupId){
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("memberId",memberId);
        param.put("groupId",groupId);
        param.put("status",SundayCommonConstants.team_status_ptz);
        Serializable count = groupMemberMapper.count(param);
        if(count!=null&&((Long)count)>0)return true;
        return  false;

    }







}
